package com.example.softwareconfigurationcoursedesign.ShopPage;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

// 使用@WebServlet注解，将ProductListServlet映射到"/ProductListServlet"路径
@WebServlet("/ProductListServlet")
public class ProductListServlet extends HttpServlet {
    // 处理GET请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int currentPage = 1;
        int pageSize = 5; // 每页显示5个商品

        // 获取请求参数，如果有页码参数，则更新当前页数
        String pageParam = request.getParameter("page");
        if (pageParam != null && !pageParam.isEmpty()) {
            currentPage = Integer.parseInt(pageParam);
        }

        // 调用ProductDAO获取当前页的商品列表
        ProductDAO productDAO = new ProductDAO();
        List<Product> products = productDAO.getProducts(currentPage, pageSize);

        // 设置请求属性，以便在JSP页面中使用
        request.setAttribute("products", products);
        request.setAttribute("currentPage", currentPage);

        // 转发到商品列表页面
        request.getRequestDispatcher("productList.jsp").forward(request, response);
    }
}